package my.io.hadoop.utils;

import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    public static DateTimeFormatter DEFAULT_DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static DateTimeFormatter YEAR_MONTH = DateTimeFormatter.ofPattern("yyyy-MM");

    public static DateTimeFormatter YEAR = DateTimeFormatter.ofPattern("yyyy");
    public static DateTimeFormatter HOUR = DateTimeFormatter.ofPattern("HH");
    public static DateTimeFormatter MONTH = DateTimeFormatter.ofPattern("MM");

    /**
     * 日期转换
     *
     * @param dateStr
     * @return {@link LocalDateTime}
     */
    public static LocalDateTime parse(String dateStr) {
        try {
            return LocalDateTimeUtil.parse(dateStr, DEFAULT_DATE_TIME);
        } catch (Exception e) {
            return null;
        }
    }

    public static LocalDateTime parse(String dateStr,String rgx) {
        try {
            return LocalDateTimeUtil.parse(dateStr, rgx);
        } catch (Exception e) {
            return null;
        }
    }

    public static String toYearMonth(LocalDateTime date) {
        return date.format(YEAR_MONTH);
    }

    public static String toYear(LocalDateTime date) {
        return date.format(YEAR);
    }

    public static String toMonth(LocalDateTime date) {
        return date.format(MONTH);
    }

    public static String toHour(LocalDateTime date) {
        return date.format(HOUR);
    }
}
